<?php
	

	$sTable = "ajax";

	$urlxml = "../resources/config.xml";
	$config= new SimpleXmlElement($urlxml,1,true);
	$host = $config->database->host;
	$dbname = $config->database->name;
	$user = $config->database->user;
	$pass = $config->database->password;
	
	/*
	 * Output
	 */
	$output = array(
		"sEcho" => intval($_POST['sEcho']),
		"iTotalRecords" => 0,
		"iTotalDisplayRecords" => 0,
		"aaData" => array()
	);
	
	$sLimit = "";
	if ( isset( $_POST['iDisplayStart'] ) && $_POST['iDisplayLength'] != '-1' )
	{
		$sLimit = "LIMIT ". $_POST['iDisplayStart'] .", ". $_POST['iDisplayLength'];
	}
	
	$sWhere ="";
	if ( isset($_POST['sSearch']) && $_POST['sSearch'] != "" )
	{
		$sWhere = "WHERE (";
		$sWhere .= "cliente LIKE '%".$_POST['sSearch']."%'";
		$sWhere .= ')';
	}
	try {
		$db = new PDO('mysql:host='.$host.';dbname='.$dbname, $user, $pass);
		$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		
		$stm = $db->query('SELECT count(*) from modulo');
		$iTotal = $stm->fetchColumn();
		$output['iTotalRecords']=$iTotal;
		$stm = $db->query(
			"SELECT 
			pkid,cliente,data,riferimento,riferimento_email,riferimento_tel,
			appserver_virtual,
			appserver_virtual_type,
			appserver_cpu,
			appserver_n_cpu,
			appserver_os,
			appserver_64_bit,
			appserver_ram_tot,
			appserver_ram_free,
			appserver_raid,
			appserver_hd,
			appserver_gb_inst,
			appserver_cluster,
			appserver_marca,
			appserver_modello,
			appserver_app1,
			appserver_app2,
			appserver_app3,
			appserver_app4,
			appserver_app5,
			dbserver_appserver,
			dbserver_presente,
			dbserver_dbrms,
			dbserver_virtual,
			dbserver_virtual_type,
			dbserver_cpu,
			dbserver_n_cpu,
			dbserver_os,
			dbserver_64_bit,
			dbserver_ram_tot,
			dbserver_ram_free,
			dbserver_raid,
			dbserver_hd,
			dbserver_gb_inst,
			dbserver_cluster,
			dbserver_marca,
			dbserver_modello,
			dbserver_app1,
			dbserver_app2,
			dbserver_app3,
			dbserver_app4,
			dbserver_app5,
			sedi_n_post_principale,
			sedi_remote,
			sedi_n_sedi,
			sedi_connettivita,
			sedi_n_post_remote,
			sedi_n_abitanti,
			backup_presente,
			backup_apposito,
			note,
			punteggio,
			valutazione
			from modulo 
			$sWhere 
			order by pkid 
			$sLimit ");
		$iFilteredTotal=$stm->rowCount();
		$output['iTotalDisplayRecords']=$iTotal;
		$stm->setFetchMode(PDO::FETCH_NUM);
		foreach($stm as $row) {
			array_push($output['aaData'],$row);
		}
		$db = null;
	} catch (PDOException $e) {
		print "Error!: " . $e->getMessage() . "<br/>";
		die();
	}
	
	
	echo json_encode( $output );
?>